/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import data.GoogleSearch;
import entities.Artwork;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import reginAPI.ReginFacade;

/**
 *
 * @author bertram
 */
public class deprecatedSearch extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       try {
                        new data.Auth().checkAuth(request);
                    } catch (exceptions.AuthException aux) {
                        System.out.println("fuckede med auth");
                        response.sendRedirect("login.html");
                    }
        Map<String, String[]> params = request.getParameterMap();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        ArrayList<Artwork> works = null;
        String searchTerm = null;


        if (request.getParameterMap().containsKey("searchterm")) {
            works = new ArrayList<Artwork>();
            searchTerm = request.getParameter("searchterm");
            ArrayList<Integer> res = new GoogleSearch().workSearch(searchTerm);
            for (Integer i : res) {
                Artwork v = ReginFacade.getArtwork(i, (String) request.getSession().getAttribute("token"));
                works.add(v);
            }
        }
        PrintWriter out = response.getWriter();


        try {
            JSONObject jReturn = new JSONObject();
            JSONArray jWorks = new JSONArray();
            
            if (searchTerm != null) {

                if (works.isEmpty()) {
                    
                    jReturn.put("status", 2);
                } else {
                    
                    jReturn.put("status", 1);
                }
                for (Artwork a : works) {
                    jWorks.put(a.toJSON());
                }
               
                jReturn.put("works", jWorks);
            }
            out.write(jReturn.toString());
            //out.write( xml.toString());
            
            
           
        }
        catch(Exception ex)
        {
            System.out.println("HEY!!! " + ex.getMessage());
        }
        finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
